package se.grp03.fmtuner.scheduler;

import java.text.ParseException;

import se.grp03.fmtuner.scheduler.io.UdpManager;

public class FMTunerScheduler {
	
	private static boolean debug = false; // Debug mode.
	private static SchedulerManager scheduler;
	
	public static void main(String[] args) throws Exception
	{		
		// Handle command-line arguments.
		for(String s : args) {
			// Debug mode.
			if(s.equalsIgnoreCase("-debug")) {
				System.out.println("Debug mode enabled. StdIO used instead of UDP.");
				debug = true;
			}
			// Quick scan.
			/*else if(s.equalsIgnoreCase("-quickscan")) {
				System.out.println("Quick scan enabled. Hourly scan now every 5 seconds.");
				quick = true;
			}*/
			// Illegal argument. List all legal arguments, and terminate.
			else {
				System.err.println("Illegal argument: " + s);
				System.err.println("[-debug] ....... Enable debug mode (STDIO instead of UDP)");
				//System.err.println("[-quickscan] ... Enable quick scan (Hourly scan every 5 secs)");
				System.err.println("FM Tuner Scheduler terminated.");
				System.exit(1);
			}
		}
		
		// Start scheduler.
		scheduler = SchedulerManager.getInstance();
		scheduler.startScheduler(debug);
    	
		// Start hourly scan job.
    	scheduleHourlyScanMessage();
    	
    	// Start socket connections to listen for incoming messages.
		UdpManager udp = UdpManager.getInstance(debug);
		udp.start();
	}
	
	/**
	 * Schedules the hourly scan event, which sends
	 * a scan message to the FM Tuner Agent.
	 */
	private static void scheduleHourlyScanMessage() {
		try {
			scheduler.scheduleHourlyScanJob();
		} catch (ParseException e) {
			System.err.println(e);
		}
	}
}